import mysql.connector
from mysql.connector import Error
import logging
from typing import List, Tuple, Optional

class TermManager:
    def __init__(self):
        self.logger = logging.getLogger(self.__class__.__name__)
        self.db_config = {
            "host": "localhost",
            "user": "mysqluser",
            "password": "lxy_Mysql@22_LinuxC1024",
            "database": "poly_model_agent_db"
        }
    
    def add_term(self, source_term: str, target_term: str, 
                 source_lang: str, target_lang: str, 
                 category: Optional[str] = None) -> bool:
        """添加新的术语对"""
        try:
            conn = mysql.connector.connect(**self.db_config)
            cursor = conn.cursor()
            
            query = """
                INSERT INTO technical_terms 
                (source_term, target_term, source_lang, target_lang, category)
                VALUES (%s, %s, %s, %s, %s)
            """
            cursor.execute(query, (source_term, target_term, 
                                 source_lang, target_lang, category))
            
            conn.commit()
            cursor.close()
            conn.close()
            
            self.logger.info(f"Added term pair: {source_term} -> {target_term}")
            return True
            
        except Error as e:
            self.logger.error(f"Failed to add term: {str(e)}")
            return False
    
    def get_terms(self, source_lang: str, target_lang: str) -> List[Tuple[str, str]]:
        """获取特定语言对的所有术语"""
        try:
            conn = mysql.connector.connect(**self.db_config)
            cursor = conn.cursor()
            
            query = """
                SELECT source_term, target_term 
                FROM technical_terms 
                WHERE source_lang = %s AND target_lang = %s
            """
            cursor.execute(query, (source_lang, target_lang))
            terms = cursor.fetchall()
            
            cursor.close()
            conn.close()
            
            return terms
            
        except Error as e:
            self.logger.error(f"Failed to get terms: {str(e)}")
            return [] 